

import Vue  from 'vue'
import {
    RECEIVE_ADDRESS, RECEIVE_CATEGORYS, RECEIVE_SHOPS, USER_INFO, LOGOUT,

    RECEIVE_GOODS,
    RECEIVE_RATINGS,
    RECEIVE_INFO,
    INCREMENT_FOOD_COUNT,
    DECREMENT_FOOD_COUNT,
    CLEAR_CART

} from './mutation-types'

// actions: {
//     increment (context) {
//       context.commit('increment')
//     }
//   }


import {
    reqAddress, reqCategorys, reqShops, reqPwdLogin, reqPhoneLogin,
    reqShopGoods,
    reqShopInfo,
    reqShopRatings



} from '../api/index'
export default {
    async getCategorys(context) {


        let result = await reqCategorys();
        //  console.log(result.data);
        let categorys = result.data
        context.commit(RECEIVE_CATEGORYS, categorys)
    },

    async getShops(context, shopData) {   //shopData 是一个经纬度 对象

        let result = await reqShops(shopData)
        console.log(result.data)
        let shops = result.data;

        context.commit(RECEIVE_SHOPS, shops)

    },

    //登录 获取用户信息
    async asyncgetuserLogin(context, userInfos) {
        let userInfo = await reqPwdLogin(userInfos)

        context.commit(USER_INFO, userInfo.data)
    },

    //手机号码短信验证登录 

    async asyncPhoneLogin(context, dataPhone) {
        let userInfo = await reqPhoneLogin(dataPhone);
        console.log(userInfo)
        if (userInfo.code == 0) {
            context.commit(USER_INFO, userInfo.data)
        }
    },


    //退出  
    getLogout(context) {

        context.commit(LOGOUT)
    },



    // 商家个人信息 

    async getShopInfo(context) {
        let result = await reqShopInfo()
        if (result.code == 0) {
            let info = result.data
            context.commit(RECEIVE_INFO, { info })
        }


    },
    //商家商品信息
    async getShopGoods(context, callback) {
        let result = await reqShopGoods()
        if (result.code == 0) {
            let goods = result.data
            context.commit(RECEIVE_GOODS, { goods })
            callback && callback()
        }


    },
    //商家评论信息
    async getShopRatings({ commit }) {
        let result = await reqShopRatings()
        if (result.code == 0) {
            let ratings = result.data
            commit(RECEIVE_RATINGS, { ratings })
        }


    },

    //购物车数据 商品数量加与减  
    updateFoodCount(context,{isflag,food}) {
        
        if(isflag) { //商品数量加1
         if(food.count) {
             food.count++
         }else {
            //  food.count=1  //不会实现页面实时双向数据绑定
            Vue.set( food,'count', 1 )

             
         
            console.log(food)


             context.state.cartFoods.push(food)
          

        
         }
            context.commit( INCREMENT_FOOD_COUNT,{cartFoods: context.state.cartFoods})
           


        }else  {//商品数量减1
            food.count--;
            if(food.count===0) {
                alert('不能在少了')

                 var myindex= context.state.cartFoods.indexOf(food);
                 console.log(myindex)

                context.state.cartFoods.splice(myindex,1)  

            }




            context.commit( DECREMENT_FOOD_COUNT,{cartFoods: context.state.cartFoods})

        }
    },



    //清空购物车 
    clear_cart({commit}) {


        commit(CLEAR_CART)
    }


}